package org.jeese.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

/**
 * 验证码工具类
 * 
 * @author wubc
 * @date 2018年9月6日 下午3:17:26
 * @version V1.0
 */
public class CaptchaUtil {
	private int width = 90;// 验证码宽度 默认值：90
	private int height = 40;// 验证码高度 默认值：40
	private int codeCount = 4;// 验证码个数 默认值：4
	private int lineCount = 20;// 混淆线个数 默认值：19
	private int fontSize = 20;// 字体大小像素
	private char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
			'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	public CaptchaUtil() {

	}

	/**
	 * 
	 * @param width    验证码宽度
	 * @param height   验证码高度
	 * @param fontSize 字体大小像素
	 */
	public CaptchaUtil(int width, int height, int fontSize) {
		this.width = width;
		this.height = height;
		this.fontSize = fontSize;
	}

	/**
	 * 
	 * @param width     验证码宽度
	 * @param height    验证码高度
	 * @param codeCount 验证码个数
	 * @param fontSize  字体大小像素
	 */
	public CaptchaUtil(int width, int height, int codeCount, int fontSize) {
		this.width = width;
		this.height = height;
		this.codeCount = codeCount;
		this.fontSize = fontSize;
	}

	/**
	 * 
	 * @param width     验证码宽度
	 * @param height    验证码高度
	 * @param codeCount 验证码个数
	 * @param lineCount 混淆线个数
	 * @param fontSize  字体大小像素
	 */
	public CaptchaUtil(int width, int height, int codeCount, int lineCount, int fontSize) {
		this.width = width;
		this.height = height;
		this.codeCount = codeCount;
		this.lineCount = lineCount;
		this.fontSize = fontSize;
	}

	public String getCode() {
		// 定义随机数类
		Random r = new Random();
		// 定义存储验证码的类
		StringBuilder builderCode = new StringBuilder();
		for (int i = 0; i < codeCount; i++) {
			char c = codeSequence[r.nextInt(codeSequence.length)];
			builderCode.append(c);
		}
		return builderCode.toString();
	}

	/**
	 * 具体获取验证码的方法
	 * 
	 * @param time time为时戳,这样的话可以避免浏览器缓存验证码
	 * @throws IOException
	 */
	public BufferedImage getBufferImage(String code) {
		// 定义随机数类
		Random r = new Random();
		// 定义画布
		BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		// 得到画笔
		Graphics g = bufferedImage.getGraphics();
		// 1.设置颜色,画边框
		g.setColor(Color.gray);
		g.drawRect(0, 0, width, height);
		// 2.设置颜色,填充内部
		g.setColor(Color.white);
		g.fillRect(1, 1, width - 2, height - 2);
		// 3.设置干扰线
		// g.setColor(Color.gray);
		for (int i = 0; i < lineCount; i++) {
			int _R = (int) Math.floor(Math.random() * 256);
			int _G = (int) Math.floor(Math.random() * 256);
			int _B = (int) Math.floor(Math.random() * 256);
			g.setColor(new Color(_R, _G, _B, 255));
			g.drawLine(r.nextInt(width), r.nextInt(width), r.nextInt(width), r.nextInt(width));
		}
		// 4.设置验证码
		g.setColor(Color.blue);
		// 4.1设置验证码字体
		g.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, fontSize));
		for (int i = 0; i < codeCount; i++) {
			g.drawString(String.valueOf(code.charAt(i)) + "", ((width / codeCount) * i + 2), height * 4 / 5);
		}
		return bufferedImage;
	}

	public static void main(String[] args) throws IOException {
		File dir = new File("c:/wubc");
		CaptchaUtil captcha = new CaptchaUtil(100, 30, 4, 50, 25);
		String code = captcha.getCode();
		File file = new File(dir, "111.jpg");
		FileOutputStream fos = new FileOutputStream(file);
		ImageIO.write(captcha.getBufferImage(code), "jpg", fos);

	}

//	@RequestMapping("/captcha")
//	public void captcha(HttpServletResponse response, HttpSession session) {
//		OutputStream out = null;
//		try {
//			// 获取创建验证码工具类实例
//			CaptchaUtil captcha = new CaptchaUtil(100, 30, 4, 30, 25);
//			String code = captcha.getCode();
//			
//			// 设置响应类型
//			response.setContentType("image/jpg");
//			// 将验证码存放在session
//			session.setAttribute("code", code);
//			// 获取验证码图片
//			BufferedImage img = captcha.getBufferImage(code);
//			out = response.getOutputStream();
//			// 通过ImageIO写出图片
//			ImageIO.write(img, "jpg", out);
//			out.flush();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			if (out != null) {
//				try {
//					out.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//		}
//	}

}
